package ch.fhnw.qualiscan.core;

import java.io.Serializable;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.fhnw.qualiscan.ApplicationData;
import eu.livotov.tpt.TPTApplication;
import ch.fhnw.qualiscan.core.View;
import ch.fhnw.qualiscan.core.db.HibernateSession;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import ch.fhnw.qualiscan.core.helpers.UserSession;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;

@SuppressWarnings("serial")
public abstract class Controller implements Serializable {
	
	public Logger logger = LoggerFactory.getLogger("Controller");
	
	public HibernateSession hibernateSession;
	public UserSession userSession;
	private ApplicationData appData;
	
	protected View view;
	
	public UserSessionHelper userSessionHelper = new UserSessionHelper();
	public ArrayList<Permissions> neededPermissions = new ArrayList<Permissions>();
	
	public Controller () {
		appData = (ApplicationData) TPTApplication.getCurrentApplication().getMainWindow().getData();
	}
	
	
	public abstract void init();
	public abstract void setView();
	public abstract void loadModels();
	public abstract void setNeededPermissions();
	
	public void loadView()  {
		appData.getBaseLayout().page.removeAllComponents();
		appData.getBaseLayout().page.addComponent(view);
		
		view.setController(this);
		
	}
	
	public void loadUserSession() {
		userSession = userSessionHelper.getUserSession();
	}
	
	public void initHibernateSession() {
		hibernateSession = new HibernateSession();
	}
	
	
	public void fragmentChanged(int fragmentId, String fragment) {
		if (fragmentId == 1) {
			int id = -1;
			try {
				id = Integer.parseInt(fragment);
			}
			catch(Exception e) {}
			
			if (id != -1) {
				view.changeTab(id);
			}
			
		}
		else {
			urlFragmentChanged(fragmentId, fragment);
		}
	}
	
	public abstract void urlFragmentChanged(int fragmentId, String fragment);
	
	
	public ApplicationData getAppData() {
		return appData;
	}

}
